import sys
import pygame
import pygame
pygame.init()

screen = pygame.display.set_mode((2512/8,304))#画布304/16=19,2512=16*157
background=pygame.image.load('.\\magic-cliffs-PREVIEWx1.png')#导入图片
run=pygame.image.load('.\\spritesheets\\run.png').convert_alpha()
idle=pygame.image.load('.\\spritesheets\\idle.png').convert_alpha()
allStatus=[#状态
            pygame.image.load('.\\spritesheets\\idle.png').convert_alpha(),
            pygame.image.load('.\\spritesheets\\run.png').convert_alpha(),
            pygame.image.load('.\\spritesheets\\jump.png').convert_alpha(),
            pygame.image.load('.\\spritesheets\\melee.png').convert_alpha(),
            pygame.image.load('.\\spritesheets\\hurt.png').convert_alpha(),
        ]
status=0

pygame.mixer.music.load(".\\bgm\\nature sketch.wav")  # 音乐
pygame.mixer.music.set_volume(0.5)# 设置音量大小0~1的浮点数
pygame.mixer.music.play() # 播放音频


screen.blit(run, (10,10))
clock = pygame.time.Clock()#创建时钟对象

x=2512/8/2-50#玩家初始x坐标,y坐标,移动速度v
y=304-16*13
v=12

gravity=5#跳跃数据
jump=False
jumpSpeed=50

start_x=0
status=0
extraWidth=0

screen_x=0
# screen_xRect=screen.get_rect()


while True:#死循环让窗口一直显示
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.KEYDOWN:  # 如果检测到按键按下
            if event.key == pygame.K_e:  # 如果键是E键
                print("E键被按下!")
                for i in range(0,1046,96):
                    clock.tick(12)
                    status = 3
                    start_x %= (1056/11*6)
                    # screen.blit(background, (0, 0))
                    screen.blit(allStatus[status], (x, y), area=(start_x, 0, 96, 64))
                    pygame.display.flip()
        gravity=5
        jumpSpeed=10
    start_x+=32


    keys_pressed = pygame.key.get_pressed()

    if keys_pressed[pygame.K_w]:
        print("W键被按下!")
        if y>304 - 16 * 13:y=304 - 16 * 13

        if y == 304 - 16 * 13:
            jumpSpeed -= 3
            y -= jumpSpeed
        else:
            gravity += 3
            y += gravity

    if screen_x+2512/8/2 <= x:
        screen_x += 10
        #x -= 10

    if keys_pressed[pygame.K_d]:
        clock.tick(13)
        print("D key is pressed")
        status=1
        #x+=13
        start_x %= 256
        screen_x+=13
    # elif keys_pressed[pygame.K_e]:
    #     print("E key is pressed")

    else:
        start_x += 32
        extraWidth=0
        clock.tick(5)
        status=0
        start_x %= 192

    screen.blit(background, (0, 0), area=(screen_x,0,2512/8,304))
    screen.blit(allStatus[status], (x, y), area=(start_x, 0, 32+extraWidth, 64))
    pygame.display.flip()



